﻿using RotovapPlatform.Domain.Equipment.Device.IO;
using RotovapPlatform.Domain.Equipment.Device.Motors.Dto;
using RotovapPlatform.Domain.Equipment.Device.Robot.Dto;
using RotovapPlatform.Domain.Equipment.Device.TCU.Dto;
using RotovapPlatform.Domain.Equipment.Device.OilBath.Dto;
using RotovapPlatform.Domain.Equipment.Device.TemperatureCollector.Dto;
using RotovapPlatform.Domain.Equipment.Device.Valves.Dto;
using RotovapPlatform.Domain.Equipment.Interfaces.Base;
using System;
using System.Threading.Tasks;
using RotovapPlatform.Domain.Common;

namespace RotovapPlatform.Domain.Equipment.Interfaces.Services
{
	/// <summary>
	/// 设备状态服务接口
	/// 定义了领域层需要的设备状态监控功能
	/// </summary>
	public interface IDeviceStatusService
	{
		/// <summary>
		/// 开始监控设备状态
		/// </summary>
		Task StartMonitoringAsync();

		/// <summary>
		/// 停止监控设备状态
		/// </summary>
		Task StopMonitoringAsync();

		/// <summary>
		/// 更新所有设备状态
		/// </summary>
		Task UpdateAllDeviceStatusAsync();

		/// <summary>
		/// 获取电机状态
		/// </summary>
		/// <param name="motorId">电机ID</param>
		/// <returns>电机状态</returns>
		Task<MotorStatus> GetMotorStatusAsync(string motorId);

		/// <summary>
		/// 获取TCU状态
		/// </summary>
		/// <param name="tcuId">TCU ID</param>
		/// <returns>TCU状态</returns>
		Task<TCUStatus> GetTCUStatusAsync(string tcuId);

		/// <summary>
		/// 获取油浴锅状态
		/// </summary>
		/// <param name="oilBathId">油浴锅ID</param>
		/// <returns>油浴锅状态</returns>
		Task<OilBathStatus> GetOilBathStatusAsync(string oilBathId);

		/// <summary>
		/// 获取温度采集模块状态
		/// </summary>
		/// <param name="collectorId">温度采集模块ID</param>
		/// <returns>温度采集模块状态</returns>
		Task<TemperatureCollectorStatus> GetTemperatureCollectorStatusAsync(string collectorId);

		/// <summary>
		/// 获取切换阀状态
		/// </summary>
		/// <param name="valveId">切换阀ID</param>
		/// <returns>切换阀状态</returns>
		Task<ValveStatus> GetValveStatusAsync(string valveId);

		/// <summary>
		/// 获取机器人状态
		/// </summary>
		/// <returns>机器人任务反馈和设备状态</returns>
		Task<(RobotTaskFeedBackDto TaskFeedback, RobotStatusDto DeviceStatus)> GetRobotStatusAsync();

		/// <summary>
		/// 获取数字输入信号
		/// </summary>
		/// <param name="id">信号ID</param>
		/// <returns>数字输入信号</returns>
		IDISignal GetDISignal(DISignalId id);

		/// <summary>
		/// 获取数字输出信号
		/// </summary>
		/// <param name="id">信号ID</param>
		/// <returns>数字输出信号</returns>
		IDOSignal GetDOSignal(DOSignalId id);

		/// <summary>
		/// 设置数字输出信号状态
		/// </summary>
		/// <param name="id">信号ID</param>
		/// <param name="state">目标状态</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> SetDOSignalStateAsync(DOSignalId id, bool state);

		/// <summary>
		/// 获取冷凝器压力值
		/// </summary>
		/// <returns>压力值，单位：mbar</returns>
		double GetCondenserPressure();

		/// <summary>
		/// 设备状态变化事件
		/// </summary>
		event EventHandler<DeviceStatusEventArgs> DeviceStatusChanged;

		/// <summary>
		/// 注册设备状态变化回调
		/// </summary>
		/// <param name="callback">回调函数</param>
		void RegisterStatusCallback(Func<string, object, Task> callback);
	}

	/// <summary>
	/// 设备状态事件参数
	/// </summary>
	public class DeviceStatusEventArgs : EventArgs
	{
		/// <summary>
		/// 设备ID
		/// </summary>
		public string DeviceId { get; }

		/// <summary>
		/// 设备状态
		/// </summary>
		public object Status { get; }

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="deviceId">设备ID</param>
		/// <param name="status">设备状态</param>
		public DeviceStatusEventArgs(string deviceId, object status)
		{
			DeviceId = deviceId;
			Status = status;
		}
	}
}